package com.edushare.common.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.cayenne.DataObjectUtils;

import com.edushare.common.orm.ORMInterface;
import com.edushare.common.vo.CourseDeptLink;
import com.edushare.common.vo.Departments;
import com.edushare.common.vo.UnivDeptLink;
import com.edushare.common.vo.Universities;

public class DepartmentsDAO extends CayenneBasedDAO {

	public DepartmentsDAO(ORMInterface ormInterface) {
		super(ormInterface);
	}
	
	public Departments getDepartmentByPk(int pk){
		return DataObjectUtils.objectForPK(getDataContext(), Departments.class, pk);
	}
	
	public Departments addNewDepartment(Universities univ, String deptName){
		Departments dept = new Departments();
		dept.setCreationTimestamp(new Date());
		dept.setDeptDesc("");
		dept.setDeptName(deptName);

		UnivDeptLink udl = new UnivDeptLink();
		udl.setAssociationType(UnivDeptLink.UNIVERSITY_DEPT_OWNER);
		udl.setUniversity(univ);
		udl.setDepartment(dept);
		
		getDataContext().registerNewObject(dept);
		getDataContext().registerNewObject(udl);
		getDataContext().commitChanges();
		return dept;
	}
	
	public boolean addExistingDept(Universities univ, Departments dept){
		UnivDeptLink udl = new UnivDeptLink();
		udl.setAssociationType(UnivDeptLink.UNIVERSITY_DEPT_SUBSCRIBER);
		udl.setDepartment(dept);
		udl.setUniversity(univ);
		getDataContext().registerNewObject(udl);
		getDataContext().commitChanges();
		return true;
	}
	
	public Departments getDeptByName(Universities univ, String name){
		List<UnivDeptLink> udl = univ.getDeptLinks();
		for(UnivDeptLink u:udl){
			if(u.getAssociationType() == UnivDeptLink.UNIVERSITY_DEPT_OWNER &&
					u.getDepartment().getDeptName().trim().equalsIgnoreCase(name.trim()))
				return u.getDepartment();
		}
		return null;
	}
	
	public Universities getParent(Departments dept){
		List<UnivDeptLink> udl = dept.getUnivLinks();
		for(UnivDeptLink u:udl){
			if(u.getAssociationType() == UnivDeptLink.UNIVERSITY_DEPT_OWNER)
				return u.getUniversity();
		}
		return null;
	}
	
	public String removeDept(Departments dept, Universities univ){
		if(getParent(dept)==univ){
			getDataContext().deleteObjects(dept.getUnivLinks());
			List<CourseDeptLink> cdList = dept.getCourseLinks();
			for(CourseDeptLink cd:cdList){
				new CoursesDAO(ormInterface).removeCourse(cd.getCourse());
			}
			getDataContext().deleteObjects(dept.getCourseLinks());
			getDataContext().deleteObjects(dept.getUserLinks());
			getDataContext().deleteObject(dept);
			getDataContext().commitChanges();
			return "Removed "+dept.getDeptName()+" from "+univ.getUniversityName();
		}
		return "";
	}
	
	public List<Departments> getDeptForUniv(Universities univ){
		List<UnivDeptLink> udlList = univ.getDeptLinks();
		List<Departments> deptList = new ArrayList<Departments>();
		for(UnivDeptLink udl:udlList){
			deptList.add(udl.getDepartment());
		}
		return deptList;
	}
	
	public String addDepartment(String deptName, Universities univ){
		if(getDeptByName(univ, deptName)!=null){
			return "Department \""+ deptName+"\" already present in "+univ.getUniversityName();
		}else{
			if(addNewDepartment(univ, deptName)!=null)
				return "Department \""+deptName+"\" added to "+ univ.getUniversityName();
			else
				return "Unable to add department \""+deptName+"\" due to unknown error";
		}
	}
}
